<?php

$thisPage = "list_businessleads";

$params = getRequestParameter("params");
$params[strlen($params)-5] = ' ';
$params[strlen($params)-6] = ' ';
$params = unserialize(stripslashes(getRequestParameter("params")));
$header = array(
    array(title => "Request to sell"),
    array(title => "Product name"),
    array(title => "Company name"),
    array(title => "Quantity"),
    array(title => "HS4 code"),
    array(title => "Origin"),
    array(title => "Destination"),
    array(title => "Posted"),
);

if (!isset($params) || $params == "") {
    $params = $_POST;
}

$pageinfo = $params['pageinfo'];

TemplateHelper::useLayout("list");

addDataservice("getpage");
addHtmlService("getpage");

loadJQueryPlugin("innerfade");
loadJQueryPlugin("textsearch");

Log::info("Loading page functions: $thisPage");

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "getpage" : { return execute_getpage(); break; }
    }
}

function buildLayout(&$smarty) {
    global $thisPage;
    global $params;

    Log::debug("Executing function: buildLayout");

    Log::debug("storing last search");
    storeParameter("lastsearch", $params);

    $result = execute_getresult($params);
    $smarty->assign("activesearchtab", "businessleads");
    if ($params['type'] == "sell") {
        $smarty->assign("searchvalue_sell", urldecode($params['text']));
    } else if ($params['type'] == "buy") {
        $smarty->assign("searchvalue_buy", urldecode($params['text']));
    }
    $smarty->assign("searchcontent", $smarty->fetch(TemplateHelper::getTemplateFile("businessleads", "bl_search")));
    Database::loadModel("companies");
    $companies = Database::getModel()->getRotatorCompaniesFor("businesslead");
    $pictures = array(
        array(src => "site/layout/searchscreen/resources/img/agendarium_logo.png", href => "http://www.lancasterconsulting.hu"),
        array(src => "site/layout/searchscreen/resources/img/youcv_banner_2.jpg", href => "http://www.lancasterconsulting.hu"),
        array(src => "site/layout/searchscreen/resources/img/lancasterconsulting_banner.png", href => "http://www.lancasterconsulting.hu"),
        array(src => "site/layout/searchscreen/resources/img/empty_rotator.png", href => "index.php?choice=mediaoffer"),
    );
    $counter = 0;
    for ($i = 0; $i < count($companies) && $i < 4; $i++) {
        if ($i == 1) { $counter++; }
        $pictures[$counter]['src'] = callFileloaderDisplayCompanyFile("export", "rotator", $companies[$i]['company_id']);
        $pictures[$counter]['href'] = $companies[$i]['hphungarian'];
        $counter++;
        if ($counter >= 4) { break; }
    }
    $smarty->assign("pictures", $pictures);
    if (!Auth::isLoggedIn()) {
        $smarty->assign("notloggedin", $smarty->fetch("pages/$thisPage/notloggedin.tpl"));
    }
    $smarty->assign("list", array(
        title => ($params['type'] == "sell" ? "Selling leads" : "Buying leads"),
        hassummaryrow => false,
//        summaryrow => getsummary($smarty, $result['resultCount']),
        pager => getpager($result['resultCount']),
        content => getcontent($smarty, $result),
    ));
    $smarty->assign("pager", false);
}

function execute_getresult($params) {
    global $pageinfo;

    initDatabase();
    Database::loadModel("companies");
    $currentPage = ($pageinfo['current_page'] ? $pageinfo['current_page'] : 1);

    $result = array();
    if (isset($params['selectedItem'])) {
        $count = 1;
        $result = array(
            Database::getItem($params['selectedItem'])
        );
    } else {
        if ($params['type'] == "buy") {
            $result = Database::getModel()->getBuyingLeadsLike($params['text']);
        } else if ($params['type'] == "sell") {
            $result = Database::getModel()->getSellingLeadsLike($params['text']);
        }
        $sponsor = array();
        for ($i = 0; $i < count($result); $i++) {
            if (dateIsPast($result[$i]['spn_product_photo'])) {
                unset($result[$i]['photo_id']);
            }
        }
    }
    return array(
        result => $result,
        resultCount => $count,
    );
}

function getSummary($smarty, $resultCount) {
    global $params;
    global $thisPage;

    if ($resultCount == 0) { return ""; }
    $smarty->assign("summary", array(
        count => $resultCount,
        freetype => ($params['hs2'] ? false : true),
        hs4code => $params['hs4'],
        productname => $params['freetext'],
    ));
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "summary"));
}

function execute_getpage() {
    global $pageinfo;
    global $smarty;

    $params = getStoredParameter("lastsearch");
    $result = execute_getresult($params);
    return getContent($smarty, $result);

}

function getheader($smarty) {
    global $thisPage;
    global $header;

    $resultHTML = "";
    for ($i = 0; $i < count($header); $i++) {
        $smarty->assign("headeritem", $header[$i]);
        $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "header"));
    }
    return $resultHTML;
}

function getpager($resultCount) {
    global $pageinfo;
    if ($resultCount == 0) { return ""; }
    return array(
        currentpage => ($pageinfo['current_page'] ? $pageinfo['current_page'] : 1),
        pagecount => ceil($resultCount / 8),
    );
}

function getContent($smarty, $result) {
    global $thisPage;
    $units = array(
        "Liters",
        "Gallons",
        "Kilograms",
        "Pieces",
        "Meters",
        "Tons",
        "Boxes",
    );
    $resultHTML = "";
    if (count($result['result']) == 0) {
        $resultHTML = $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "noresult"));
    } else {
        for ($i = 0; $i < count($result['result']); $i++) {
            $result['result'][$i]['unit'] = $units[$result['result'][$i]['unit'] - 1];
            if ($i % 5 == 0 && $i > 0) {
                $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "ad"));
            }
            $smarty->assign("item", $result['result'][$i]);
            $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contentitem"));
        }
    }
    return $resultHTML;
/*
    global $thisPage;
    global $params;

    $resultHTML = "";
    if (count($result['result']) == 0) {
        $smarty->assign("request", $params['text']);
        $smarty->assign("type", $params['type']);
        $resultHTML = $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content_empty"));
    } else {
        for ($i = 0; $i < count($result['result']); $i++) {
            if (($i+1) % 2 == 0) { $smarty->assign("oddeven", "even"); } else { $smarty->assign("oddeven", "odd"); }
            $smarty->assign("item", $result['result'][$i]);
            $resultHTML .= $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contentitem"));
        }
    }
    return $resultHTML;
*/
}

function getPageHeader() {
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    return TemplateHelper::getDefaultPageFooter(array(
        type => "simple"
    ));
}

function getLeftSide($smarty) {
    global $thisPage;
    global $_CONFIG;

    $smarty->assign("leftpictures", array(
    ));
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "leftpictures"));
}

function getTop($smarty) {
    global $thisPage;

    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "toppictures"));
}

function getBottom($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "bottom"));
}
